package com.bdf.ipharma.gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;

import com.bdf.ipharma.config.Session;
import com.bdf.ipharma.model.ProductoDispenser;
import com.bdf.ipharma.model.map.ProductoStockMap;
import com.bdf.ipharma.starter.McdPrs;
import com.bdf.ipharma.util.Utils;

public class GUIPopUpCounter extends JDialog implements ActionListener {
	
	private Session session;
	private ProductoStockMap producto;
	private Component component;
	private ProductoDispenser prodDispenser; 
	
	private JTextField textField;
	private RoundedPanel contentPanel; 
	private GUIButtonImage jBtnAdd;
	private GUIButtonImage jBtnLess;
	private JLabel jLblTitle;
	private static Point point = new Point();
	private GuiExtraction2 guiExtraction2;
	
	public GUIPopUpCounter(Session session,Component component) {
		this.session=session;
		this.component= component;
		init();
	}

	
	public void init(){
		
		
		if(component instanceof GuiExtraction2){
			this.guiExtraction2 = (GuiExtraction2) this.component;
			this.guiExtraction2.setEnabled(false);
		}
		
		Utils.locateWindowAtCenter(guiExtraction2,this);
		
		this.setSize(320,200);
		this.setUndecorated(true);
		
		contentPanel = new RoundedPanel();
		contentPanel.setImg(Images.bgExtractionPopUp.getImage());
		contentPanel.setBackground(new Color(255, 255, 255));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addComponent(contentPanel, GroupLayout.PREFERRED_SIZE, 317, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addComponent(contentPanel, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
		);
		
		jBtnAdd = new GUIButtonImage("+",Images.btnAdd,Images.btnAddDn);
		jBtnAdd.setFont(new Font("Tahoma", Font.PLAIN, 18));
		jBtnAdd.setForeground(new Color(255, 255, 255));
		
		textField = new JTextField();
		textField.setHorizontalAlignment(SwingConstants.CENTER);
		textField.setFont(new Font("Tahoma", Font.BOLD, 20));
		textField.setColumns(10);
		textField.setEditable(false);
		
		jBtnLess = new GUIButtonImage("-",Images.btnBottom,Images.btnBottomDn);
		jBtnLess.setFont(new Font("Tahoma", Font.PLAIN, 18));
		jBtnLess.setForeground(new Color(255, 255, 255));
		
		GUIButtonImage JBtnAceptar = new GUIButtonImage(Texts.BTN_OK,Images.buttOk,Images.buttOkDn);
		
		
		GUIButtonImage jBtnCancelar = new GUIButtonImage(Texts.BTN_CANCEL,Images.buttOk,Images.buttOkDn);
		
		
		jLblTitle = new JLabel();
		jLblTitle.setForeground(new Color(255, 255, 255));
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.TRAILING, false)
								.addComponent(jBtnAdd, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(jBtnLess, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(textField, GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE))
							.addGap(39)
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
								.addComponent(JBtnAceptar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(jBtnCancelar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addGap(89))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(jLblTitle, GroupLayout.DEFAULT_SIZE, 297, Short.MAX_VALUE)
							.addContainerGap())))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addComponent(jLblTitle)
					.addGap(11)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(2)
							.addComponent(jBtnAdd, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(textField, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(jBtnLess, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(46)
							.addComponent(JBtnAceptar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(jBtnCancelar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(200, Short.MAX_VALUE))
		);
		contentPanel.setLayout(gl_contentPanel);
		getContentPane().setLayout(groupLayout);
		
		
		 this.addMouseListener(new MouseAdapter() {
		      public void mousePressed(MouseEvent e) {
		        point.x = e.getX();
		        point.y = e.getY();
		      }
		    });
		    this.addMouseMotionListener(new MouseMotionAdapter() {
		      public void mouseDragged(MouseEvent e) {
		        Point p = getLocation();
		        setLocation(p.x + e.getX() - point.x, p.y + e.getY() - point.y);
		      }
		    });
		    
		   //Buttons Action 
		    
		    jBtnAdd.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					session.setTimeout(new Date());
					int cantidad = Integer.parseInt(textField.getText())+1;
					//textField.setText(String.valueOf(cantidad));
					
					if(cantidad <= producto.getCantidad()){
						textField.setText(String.valueOf(cantidad));
					}else{
						//MENSAJES
						JOptionPane.showMessageDialog(
								JOptionPane.getFrameForComponent((Component)e.getSource()),
								McdPrs.MSG_POPUP_1.replace("{0}", producto.getCantidad().toString()));
					}
					
				}
			});
			
			jBtnLess.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {										
					session.setTimeout(new Date());
					
					int cantidad = Integer.parseInt(textField.getText())-1;
					if(cantidad >= 0){
						textField.setText(String.valueOf(cantidad));
					}else{
						JOptionPane.showMessageDialog(
								JOptionPane.getFrameForComponent((Component)e.getSource()),
								McdPrs.MSG_POPUP_2);
					}
				}
			});
		    jBtnCancelar.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
				  setVisible(false);
				}
			});
		    
		    JBtnAceptar.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					session.setTimeout(new Date());
					List<ProductoStockMap> selectedProducts=null; 
					producto.setCantidadAExtraer(new Integer(textField.getText()));
					if(session.get(McdPrs.PRODUCTOS_SELECCIONADOS)!=null){
						selectedProducts = (List<ProductoStockMap>) session.get(McdPrs.PRODUCTOS_SELECCIONADOS);
						session.remove(McdPrs.PRODUCTOS_SELECCIONADOS);
						session.put(McdPrs.PRODUCTOS_SELECCIONADOS,Utils.changeProductStockMapValues(selectedProducts, producto));
					}else{
						selectedProducts = new ArrayList<ProductoStockMap>();
						session.put(McdPrs.PRODUCTOS_SELECCIONADOS,selectedProducts.add(producto));
					}
					setVisible(false);
					
					if(guiExtraction2!=null){
						guiExtraction2.initTable(true);
					}
				
				}
			});
		    
	}
	
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	public ProductoStockMap getProducto() {
		return producto;
	}

	
	
	public Session getSession() {
		return session;
	}

	public void setSession(Session session) {
		this.session = session;
	}

	public void setProducto(ProductoStockMap producto) {
		this.producto = producto;
		this.jLblTitle.setText("Extraccion de "+this.producto.getDescripcion());
		this.textField.setText(this.producto.getCantidad().toString());
	}
	
	public static void main(String[] args) {
		GUIPopUpCounter frame = new GUIPopUpCounter(null,null);
		frame.setVisible(true);
	}
}
